//
//  LoginViewController.m
//  My - douban
//
//  Created by dllo on 15/11/1.
//  Copyright (c) 2015年 张鸿峰. All rights reserved.
//

#import "LoginViewController.h"

#import "RegistViewController.h"

#import "DataBaseSingleTon.h"

@interface LoginViewController ()

@property (nonatomic,retain) UIButton *loginButton;
@property (nonatomic,retain) UIButton *registButton;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor brownColor];
    
    self.navigationItem.title = @"用户登录";
    
    self.userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 100, 40)];
    [self.view addSubview:self.userNameLabel];
    self.userNameLabel.text = @"用户名:";
    [self.userNameLabel release];
    
    self.userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(140, 80, 150, 40)];
    self.userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.userNameTextField.placeholder = @"请输入用户名";
    self.userNameTextField.clearButtonMode = UITextFieldViewModeAlways;
    [self.view addSubview:self.userNameTextField];
    [self.userNameTextField release];
    
    self.pwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.userNameLabel.frame.origin.x, self.userNameLabel.frame.origin.y + self.userNameLabel.frame.size.height + 20, self.userNameLabel.frame.size.width, self.userNameLabel.frame.size.height)];
    self.pwdLabel.text = @"密码:";
    [self.view addSubview:self.pwdLabel];
    [self.pwdLabel release];
    
    self.pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.userNameTextField.frame.origin.x, self.pwdLabel.frame.origin.y, self.userNameTextField.frame.size.width, self.userNameTextField.frame.size.height)];
    self.pwdTextField.placeholder = @"请输入密码";
    self.pwdTextField.secureTextEntry = YES;
    self.pwdTextField.clearButtonMode = UITextFieldViewModeAlways;
    self.pwdTextField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.pwdTextField];
    [self.pwdTextField release];
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.loginButton.frame = CGRectMake(20, self.pwdLabel.frame.origin.y + 80, 80, 40);
    [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    self.loginButton.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.loginButton];
    [self.loginButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.registButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.registButton.frame = CGRectMake(self.loginButton.frame.origin.x + self.loginButton.frame.size.width + 80, self.loginButton.frame.origin.y, self.loginButton.frame.size.width, self.loginButton.frame.size.height);
    [self.registButton setTitle:@"注册" forState:UIControlStateNormal];
    self.registButton.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.registButton];
    [self.registButton addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //loginButton和registButton两个按钮调用同一个方法
    
}

- (void)UIAlertController
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不存在" preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertC animated:YES completion:^{
        
        
    }];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        
    }];
    
    [alertC addAction:action];
}



- (void)ButtonAction:(UIButton *)btn
{
    if (self.loginButton == btn) {  //如果是登录按钮
        
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM user"];
        NSMutableArray *arrM = [[DataBaseSingleTon shareDataBase] selectUserSQL:sql];
        BOOL result1 = [arrM containsObject:self.userNameTextField.text];
        
        NSLog(@"result1 = %d",result1);
        
        if (result1) {
            
//            NSLog(@"222");
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:self.userNameTextField.text forKey:@"userName1"];
            [userDefaults synchronize];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        else
        {
            [self UIAlertController];
        }
    }
    if (self.registButton == btn) {   //如果是注册按钮
        
        RegistViewController *registVC = [[RegistViewController alloc] init];
        [self.navigationController pushViewController:registVC animated:YES];
        
        [registVC release];
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
